import { _decorator, Component, input, KeyCode, Node, Input, EventKeyboard } from 'cc';
import { EventUtil } from '../../core_cc/EventUtil';
import EventName from './EventName';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {


    @property(Node)
    btn_left: Node = null;

    @property(Node)
    btn_right: Node = null;

    protected onLoad(): void {
        this.btn_left.click(this.onLeft, this);
        this.btn_right.click(this.onRight, this);
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    protected onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    onLeft() {
        EventUtil.emit(EventName.left, EventName.left);
    }
    onRight() {
        EventUtil.emit(EventName.right, EventName.right);
    }

    onKeyDown(event: EventKeyboard) {
        const keyCode = event.keyCode;
        if (keyCode == KeyCode.ARROW_RIGHT || keyCode == KeyCode.KEY_D) {
            this.onRight();
        }
        if (keyCode == KeyCode.ARROW_LEFT || keyCode == KeyCode.KEY_A) {
            this.onLeft();
        }
    }

}


